/**
 * A customizable timer that allows you to keep track of in-game time.
 * To use in an actor, simply declare a new Timer.
 * 
 * Example usage:
 * 
 * public class Player extends Actor
 * {
 *     private Timer timer=new Timer();
 *     ...
 *     
 *     public Player
 *     {
 *         ...
 *     }
 *     
 *     act()
 *     {
 *         timer.tick();
 *         ...
 *     }
 *     
 *     ...
 *     
 * }
 * 
 * Since it runs along Greenfoot, your time based ineractions won't be broken when Greenfoot is
 * paused nor stopped.
 * 
 * @author Jesus Trejo
 * @version 2.1, 17/May/14
 */
public class Timer
{
    private boolean isWaiting;
    private boolean isCounting;
    private float time;
    
    public Timer()
    {
        isWaiting=false;
        isCounting=false;
        time=0;
    }
    
    /**
     * Increases the timer by a set amount (needs to be adjusted manually 
     * depending on your game speed). It MUST to be called in every actor's act cycle a single
     * time (and only one) in order to work properly.
     */
    
    public void tick()
    {
        time+=0.016;
    }
    
    /**
     * Returns the actual time.
     */
    
    public int getTime()
    {
        return((int)(time*1000));
    }
    
    /**
     * Resets the time to 0, so it can start a new count.
     */
    public void mark()
    {
        time=0;
    }
        
    /**
     * Checks if the timer is waiting.
     * Receives as parameter the time it should wait in milliseconds, it'll automatically
     * mark and check if the time elapsed is minor than the time it should wait.
     * 
     * Returns true if it's waiting (time elapsed < time it should wait) and false
     * if the time elapsed is bigger or equal.
     * 
     * Examples: if(timer.wait(1000))
     *          return;
     *          
     *          if(!timer.wait(50))
     *          turn(10);
     *          
     * In the first example, the timer will return true for ~1 second, after that, it'll return false.
     * If it was called in an Actor's act(), it'll return true for 1 second, after
     * that, it'll return false for a fraction of second, then true once again since it'll be
     * automatically reset.
     * 
     * In the second example, the actor will turn 10 degrees aproximately every 50 milliseconds.
     */
    
    public boolean wait(int t)
    {
        if(isWaiting==false)
        {
            isWaiting=true;
            mark();
        }
        
        if(getTime() < t)
            return(true);
        else
        {
            isWaiting=false;
            return(false);
        }
    }
    
    /**
     * Resets the timer.
     */
    
    public void reset()
    {
        isWaiting=false;
        isCounting=false;
        time=0;
    }
    
    /**
     * Starts a count. It can be called multiple times without being reset. Use the method reset()
     * to start a new count (must to be reset manually unlike wait()).
     */
    public void count()
    {
        if(isCounting==false)
        {
            time=0;
            isCounting=true;
        }
    }
    
    /**
     * Changes the current time to a given value
     */
    public void setTime(int num)
    {
        time=num;
    }   
}